package controller.manufacture;

import java.sql.SQLException;

import service.CoffeeService;
import to.JobTO;
import util.CoffeeUtil;

public class MixingMachine extends Thread {

	private static CoffeeService service;

	public MixingMachine(CoffeeService service) {
		this.service = service;
	}

	@Override
	public void run() {
		while (true) {
			int job;
			try {
				job = service.selectMachineWaitingJob("혼합기");
				while (job > 0) {
					System.out.println("혼합기 job = "+job);
					String recipe = "B";

					try {
						service.updateMachineWaitingJob("혼합기", -1);
						service.updateMachineStatus("혼합기", "작업중");
						service.updateMachinePrograssRecipe("혼합기", recipe);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					String sTime = CoffeeUtil.getNow();
					CoffeeUtil.sleep(CoffeeUtil.MIXER_TIME);
					int mixResult = 0;
					// b-블랙,s-설탕,m-밀크
					switch (recipe) {
					case "B":
						mixResult = 1050;
						// 원액 -

						try {
							service.updateMaterialTotalWeight("원액", -1050);
						} catch (SQLException e) {
							e.printStackTrace();
						}
						break;
					case "S":
						mixResult = 1050 + 60;
						// 설탕 -
						try {
							service.updateMaterialTotalWeight("원액", -1050);
							service.updateMaterialTotalWeight("설탕", -60);
						} catch (SQLException e) {
							e.printStackTrace();
						}
						break;
					case "M":
						mixResult = 1050 + 60 + 120;
						// 설탕 프림 -
						try {
							service.updateMaterialTotalWeight("원액", -1050);
							service.updateMaterialTotalWeight("설탕", -60);
							service.updateMaterialTotalWeight("프림", -120);
						} catch (SQLException e) {
							e.printStackTrace();
						}
						break;
					}
					String eTime = CoffeeUtil.getNow();
					// 완료작업량,작업시간 입력
					JobTO jto = new JobTO("혼합", sTime, eTime);
					jto.setWorkLoad(mixResult);
					jto.setArticleType(String.valueOf(recipe));
					service.insertJobByArticleTypeInfo(jto);
					// 레시피가 다르면 일시정지 가지고 있음
					// while(recipe != ){//recipe != DB에 용기포장 작업레시피 get
					// CoffeeUtil.sleep(1000);//1초마다 체크
					// }

					// 용기포장 레시피 대기작업 + 대기작업량은 ((result*1000)/300)/10
					// 대기작업 +
					try {
						service.updateMachineWaitingJob("용기포장기",
								((mixResult * 1000) / 300) / 10);
						service.updateMachinePrograssRecipe("용기포장기", recipe);
					} catch (SQLException e) {
						e.printStackTrace();
					}
					clean();
					// 상태 - 대기중
					try {
						service.updateMachineStatus("혼합기", "대기중");
					} catch (SQLException e) {
						e.printStackTrace();
					}
					job = service.selectMachineWaitingJob("혼합기");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	static public void clean() {
		// 상태 - 청소중
		try {
			String startTime = CoffeeUtil.getNow();
			service.updateMachineStatus("혼합기", "청소중");
			CoffeeUtil.sleep(CoffeeUtil.MIXER_CLEAN_TIME);// 2분
			String endTime = CoffeeUtil.getNow();
			JobTO jto = new JobTO("혼합기 청소", startTime, endTime);
			service.insertJobInfo(jto);
			service.updateMachineStatus("혼합기", "대기중");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
